<script setup>
import { ref, computed } from "vue";
import user from '@/services/user'
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { useAllStoreData} from '@/stores/index'
import {userStore} from '@/stores/userStore'

const getImageUrl = (user) => {
  return new URL(`../assets/images/${user}.jpg`, import.meta.url).href;
};

const store = useAllStoreData()
const uStore = userStore()
const router = useRouter()
const handleLogout = async () => {
  const res = await user.logout()
  if(res.code === 200) {
    ElMessage({
      type:'success',
      message: '退出成功'
    })

    store.state.currentTag = null
    localStorage.removeItem('userState')
    router.push('/login')
  }
}

const menu = computed(()=>store.state.currentTag)

const handleCollapse = () =>{
  
  if(store.state.isCollapse) {
    store.state.isCollapse = false
  }else{
    store.state.isCollapse = true
  }
}

const handlePersonalCenter = () => {
  router.push('/personal/center')
}

</script>

<template>
  <div class="header">
    <div class="l-content">
      <div class="l-button" @click="handleCollapse">
        <component class="icons" is="Menu"></component>
      </div>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>

        <el-breadcrumb-item v-if="menu" :to="menu.path">{{menu.label}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="r-content">
      <el-dropdown>
        <span class="el-dropdown-link">
          <el-avatar :size="50" :src="uStore.state.loginUserInfo?.avatar" fit="fill"/>
           <!-- <el-avatar>user</el-avatar> -->
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item > <span @click="handlePersonalCenter">个人中心</span></el-dropdown-item>
            <el-dropdown-item ><span @click="handleLogout">退出</span></el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<style lang="less" scoped>
.icons {
  width: 20px;
  height: 20px;
  color: #fff;
}
.header {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.l-content {
  display: flex;
  align-items: center;
  .l-button {
    width: 20px;
    height: 20px;
    margin-right: 20px;
  }
}

:deep(.el-breadcrumb span) {
  color: #fff !important;
  cursor: pointer !important;
}

.el-dropdown-link:focus {
  outline: none;
}
</style>
